package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.entity.RentalEBike;
import org.jeecg.modules.rental.entity.RentalFaultType;
import org.jeecg.modules.rental.mapper.RentalFaultTypeMapper;
import org.jeecg.modules.rental.service.IRentalFaultTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: rental_fault_type
 * @Author: jeecg-boot
 * @Date: 2024-07-23
 * @Version: V1.0
 */
@Api(tags = "故障类别")
@RestController
@RequestMapping("/rental/fault-type")
@Slf4j
public class RentalFaultTypeController extends JeecgController<RentalFaultType, IRentalFaultTypeService> {

	@Autowired
    private IRentalFaultTypeService rentalFaultTypeService;
    @Autowired
    private RentalFaultTypeMapper rentalFaultTypeMapper;

    /**
     * 分页列表查询
     *
     * @param rentalFaultType
     * @param pageNo
     * @param pageSize
     * @return
     */
    //@AutoLog(value = "rental_fault_type-分页列表查询")
    @ApiOperation(value = "rental_fault_type-分页列表查询", notes = "rental_fault_type-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent="rental/keep/typelist")
    public Result<IPage<RentalFaultType>> queryPageList(RentalFaultType rentalFaultType,  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest request) {
        QueryWrapper<RentalFaultType> queryWrapper = QueryGenerator.initQueryWrapper(rentalFaultType, request.getParameterMap());
        Page<RentalFaultType> page = new Page<>(pageNo, pageSize);
        IPage<RentalFaultType> pageList = rentalFaultTypeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "rental_fault_type", notes = "rental_fault_type")
    @GetMapping(value = "/typelist")
    public Result<List<RentalFaultType>> typelist() {
        LambdaQueryWrapper<RentalFaultType> queryWrapper = new LambdaQueryWrapper<RentalFaultType>();
        List<RentalFaultType> faultTypeList = rentalFaultTypeMapper.selectList(queryWrapper);
        return Result.OK(faultTypeList);
    }

    /**
     * 添加
     *
     * @param rentalFaultType
     * @return
     */
    @AutoLog(value = "rental_fault_type-添加")
    @ApiOperation(value = "rental_fault_type-添加", notes = "rental_fault_type-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody RentalFaultType rentalFaultType) {
        rentalFaultTypeService.save(rentalFaultType);
        return Result.OK("common.add.success");
    }

    /**
     * 编辑
     *
     * @param rentalFaultType
     * @return
     */
    @AutoLog(value = "rental_fault_type-编辑")
    @ApiOperation(value = "rental_fault_type-编辑", notes = "rental_fault_type-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody RentalFaultType rentalFaultType) {
        rentalFaultTypeService.updateById(rentalFaultType);
        return Result.OK("common.edit.success");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rental_fault_type-通过id删除")
    @ApiOperation(value = "rental_fault_type-通过id删除", notes = "rental_fault_type-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        rentalFaultTypeService.removeById(id);
        return Result.OK("common.delete.success");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "rental_fault_type-批量删除")
    @ApiOperation(value = "rental_fault_type-批量删除", notes = "rental_fault_type-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.rentalFaultTypeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("common.delete.batch.success");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "rental_fault_type-通过id查询")
    @ApiOperation(value = "rental_fault_type-通过id查询", notes = "rental_fault_type-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<RentalFaultType> queryById(@RequestParam(name = "id", required = true) String id) {
        RentalFaultType rentalFaultType = rentalFaultTypeService.getById(id);
        if (rentalFaultType == null) {
            return Result.error("common.query.not.found", null);
        }
        return Result.OK(rentalFaultType);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param rentalFaultType
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalFaultType rentalFaultType) {
        return super.exportXls(request, rentalFaultType, RentalFaultType.class, "rental_fault_type");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalFaultType.class);
    }

}
